package polimi.sc.expoquest.screen.singleplay;

import polimi.sc.expoquest.QuestApplication;
import polimi.sc.expoquest.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;

import polimi.sc.expoquest.screen.DashboardActivity;
import polimi.sc.expoquest.storage.datasources.PlayedGameDataSource;
import polimi.sc.expoquest.storage.datasources.SingleGameDataSource;
import polimi.sc.expoquest.storage.model.SingleGame;
import polimi.sc.expoquest.storage.model.PlayedGame;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Random;

import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;


public class SingleplayCommentActivity extends Activity {
	//The single game datasource
	private SingleGameDataSource singleGameDS;
	//The play game data source
	private PlayedGameDataSource playedGameDS;
	//the comment being display to the user
	private SingleGame commentGame;
	//Max point Integer	
	private int maxpoint;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_singleplay_comment);
		//The rating bar
		RatingBar barStatus = (RatingBar)findViewById(R.id.ratingBarExpo);
		barStatus.setEnabled(false);
			
		//1.Getting a comment game available in the database
		commentGame = getRandomCommentGame();
		if(commentGame!=null){				
			//1.We display the game description
			TextView tvComment = (TextView)findViewById(R.id.comment_text);
			String comment = " " + commentGame.getDescription() ;
			tvComment.setText(comment);
			//2. Loading the image related to the game
			ImageView image = (ImageView) findViewById(R.id.imageGame);
			String path=commentGame.getPicURI();
			if(path!=null)
			{
				try {
					Drawable d = loadImage(path);
					image.setImageDrawable(d);
				} catch (IOException e) {}
			}
			maxpoint = commentGame.getMaxPoints();	
		}
	}
	/**
	 * Rolling the dice again
	 * @param v
	 */
	public void rollBtnPressed(View v)
	{
		Random generator = new Random();
		
		int random = generator.nextInt(3); // find a random number between 0,1,2		
		
		if (random == 0)
		{
			Intent dashbordIntent = new Intent(this, SingleplayTriviaActivity.class);
	        startActivity(dashbordIntent);
		}
		else if (random == 1)
		{
			Intent dashbordIntent = new Intent(this, SingleplayCommentActivity.class);
	        startActivity(dashbordIntent);
		}
		else if (random == 2)
		{
			Intent dashbordIntent = new Intent(this, SingleplayCameraActivity.class);
	        startActivity(dashbordIntent);
		}

	}
	/**
	 * Managing the display of the starts related to the points of the game
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_singleplay_comment, menu);
		return true;
	}
	/**
	 * Submiting the comment
	 * @param V
	 */
	public void btnsubmitClick(View V)
	{
		//1. Retrieving the comment of the user
		EditText comment = (EditText)findViewById(R.id.commentInput);	
		String commentStr = comment.getText().toString();
		//2. Assigning the points gained
		Random generator = new Random();
		int points = generator.nextInt(commentGame.getMaxPoints());
		//3.Inserting the comment game played
		if(playedGameDS == null)
			playedGameDS = new PlayedGameDataSource(this);	
		playedGameDS.open();		
		//4. Retrieving the user ID logged into the application
		QuestApplication questApp = (QuestApplication)getApplicationContext();
		int userID = questApp.getUserID();
		//5. Inserting the game
		PlayedGame pl =  playedGameDS.insertPlayedComment(commentStr,points, userID, commentGame.getId());	
		playedGameDS.close();
		//6. Showing the success of the insertion
		String commentSubmition= "Data of the play game inserted:\n" + " id: " + pl.getId()+ "\n comment: "+ pl.getComment()+ "\n points "+ pl.getPoints(); 
		Toast toast = Toast.makeText(this, commentSubmition , Toast.LENGTH_LONG);
		toast.show();
		//7.Giving the points to the user
		RatingBar bar = (RatingBar)findViewById(R.id.ratingBarExpo);
		bar.setRating(maxpoint/5);
	}
	
	//--------------------
	// Auxiliar methods
	//--------------------
	
	private SingleGame getRandomCommentGame()
	{
		//1. Creating the data source if it is not created already	
		if(singleGameDS== null)
			singleGameDS = new SingleGameDataSource(this);
		//1.1 opening the data source
		singleGameDS.open();
		//2. Obtaining the view of the results of the rolling
		Collection<SingleGame> commentGames = singleGameDS.getSingleGameByType(SingleGame.TYPE_COMMENT);
		//3. Closing the database connection
		singleGameDS.close();
		//4. Choosing one comment randomly
		Random generator = new Random();
		//4.1 getting the total comments available in the 
		int totalComments = commentGames.size();
		//4.2 We choose one index randomly
		int random=0;
		SingleGame g = null;
		try {
			random = generator.nextInt(totalComments);
			//4.3 we retrieve the game in the index selected
			Object [] o = commentGames.toArray();
			g = (SingleGame)o[random];
		}
		catch(IllegalArgumentException e){}
		return g;
		
	}
	/**
	 * load the image given on the path, the image SHOULD BE
	 * on the assets folder of the application
	 * @throws IOException 
	 */
	private Drawable loadImage(String path) throws IOException
	{
		// get input stream
		InputStream ims = getAssets().open(path);
		// load image as Drawable
		Drawable d = Drawable.createFromStream(ims, null);
		return d;
	}
	/**
	 * Going back to the dashboard page
	 * @param v
	 */
	public void homepagebtn_click(View v){
		Intent dashbordIntent = new Intent(this, DashboardActivity.class);
        startActivity(dashbordIntent);
	}
}
